package com.hx.util.page;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class Page<V> implements Serializable {

	private static final long serialVersionUID = 7853834513308510202L;

	protected int pageNo = 1; // 当前页码

	protected int pageSize = 10;// 10; //每页记录数. webPage15,数据型应用10~25

	protected int totalCount = -1; // 总记录数

	protected int pageNumber = 0; // 总页码数

	protected int pageCount = 1; //

	protected int startRow;

	protected int endRow;

	private List<V> recordsList = new ArrayList<V>();
	
	public Page() {
	}

	/**
	 * @param currentPage
	 *            The currentPage to set.
	 */
	public void setCurrentPage(Integer cPage) {
		if ((cPage == null) || (cPage.intValue() <= 0)) {
			this.pageNo = 1;
		} else {
			this.pageNo = cPage;
		}
		setStartEndRow();
	}

	private void setStartEndRow() {
		this.startRow = this.getPageSize() * (this.getPageNo() - 1) + 1;
		this.endRow = this.startRow + this.getPageSize() - 1;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int iPageNo) {

		this.pageNo = iPageNo;

		// 检查输入参数是否为空
		if (iPageNo < 1) {
			this.pageNo = 1;
		}
		int pageNum = getPageNumber();
		if (pageNum > 0 && iPageNo > pageNum) {
			this.pageNo = pageNum;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int iPageSize) {
		if (iPageSize >= 1) {
			this.pageSize = iPageSize;
		}
	}

	public int getTotalCount() {
		return this.totalCount == -1 ? 0 : totalCount;
	}

	public void setTotalCount(int iTotalCount) {
		if (iTotalCount > 0) {
			this.totalCount = iTotalCount;
		}
	}

	public int getPageNumber() {
		int mod = -1;

		if (totalCount != -1) {
			if (totalCount > pageSize) {
				mod = totalCount % pageSize;

				if (mod != 0) {
					pageNumber = (totalCount / pageSize) + 1;
				} else {
					pageNumber = (totalCount / pageSize);
				}
			} else {
				pageNumber = 1;
			}
		}

		return pageNumber;
	}

	public void setPageNumber(int iPageNumber) {
		this.pageNumber = iPageNumber;
	}
	public List<V> getRecordsList() {
		return recordsList;
	}

	public void setRecordsList(List<V> _list) {
		if ((_list != null) && (_list.size() > 0)) {
			this.recordsList = _list;
		}
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int iPageCount) {
		this.pageCount = iPageCount;
	}

	public boolean hasNext() {
		if (this.getPageNo() >= this.getPageNumber()) {
			return false;
		} else {
			return true;
		}
	}

	public boolean hasPrevious() {
		if (this.getPageNo() <= 1) {
			return false;
		} else {
			return true;
		}
	}

	public int getPrePage() {
		if (this.pageNo < 2)
			return 1;
		return this.pageNo - 1;
	}

	public int getNextPage() {
		if (this.pageNo >= this.pageNumber)
			return this.pageNumber;
		return this.pageNo + 1;
	}

	public int getStartRow() {
		this.startRow = (this.pageNo - 1) * this.pageSize + 1;

		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public int getEndRow() {
		this.endRow = this.pageNo * this.pageSize;
		if (this.endRow > this.getTotalCount() && this.getTotalCount()>0)
			this.endRow = this.getTotalCount();
		return endRow;
	}
	
	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}
	

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Page [recordsList.size="
				+ (recordsList == null ? 0 : recordsList.size())
				+ ", totalCount=" + totalCount + "]";
	}
}